
package com.cse210.grocerwe.server;

import org.restlet.resource.ServerResource;
import com.cse210.grocerwe.common.*;
import com.cse210.grocerwe.client.*;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.restlet.resource.Post;
import org.restlet.resource.Put;

public class APIResourceC extends ServerResource implements
APIInterfaceC
{
	PersistenceManager pm;
	

    @Post
    public SItem[] retrieveList()
    {
    	pm = PMF.get().getPersistenceManager();
    	Query q = pm.newQuery(ShoppingItem.class);
    	q.setFilter("group == x");
    	q.declareParameters("String x");
	    
	    @SuppressWarnings("unchecked")
		List<ShoppingItem> inventory = (List<ShoppingItem>) q.execute((String) getRequest().getAttributes().get("group"));
	    SItem[] sInventory = new SItem[inventory.size()];
	    
	    for( int i = 0; i < inventory.size();i++ )
	    {
	    	ShoppingItem si = inventory.get(i);
	    	sInventory[i] = si.toSItem();
	    }
	    pm.close();
	    return sInventory;
    }
    
    @Put
    public void createList(String user,SGroup group)
    {
    	pm = PMF.get().getPersistenceManager();
    	ShoppingGroup sg = new ShoppingGroup(group);
        try {
            pm.makePersistent(sg);
            MapUserToGroup mug = new MapUserToGroup(user, sg.getKey());
            pm.makePersistent(mug);
        } 
        finally {
            pm.close();
        }
    }
    	

}
